<script>
export default {
    onLaunch: async function (data) {
        // console.log(data);
        // this.$comFun.clearUserInfo();

        // #ifdef H5
        if (data.path == undefined) {
            if (process.env.NODE_ENV == 'development') {
                uni.switchTab({
                    url: '/'
                });
            } else {
                uni.redirectTo({
                    url: '/'
                });
            }
        }
        this.$store.dispatch('setDeviceType', 'h5');
        // #endif

        // #ifdef APP-PLUS
        const that = this;
        uni.getSystemInfo({
            success: function (res) {
                // 客户端平台，值域为：ios、android  
                // console.log(res);
                that.$store.dispatch('setDeviceType', res.platform);
            }
        });

        // 获取是否为上线包
        // const res_online = await this.$request.get('public/getConfigInfo', {
        //     key: 'EXAM_VERSION',
        // });
        // // console.log(res_online);
        // plus.runtime.getProperty(plus.runtime.appid, function (wgtinfo) {
        //     // console.log(wgtinfo);
        //     const versionCode = wgtinfo.versionCode;
        //     if (res_online.code == 200 && Number(res_online.result) == Number(versionCode)) {
        //         that.$store.dispatch('setIsOnline', 2);
        //     }
        // });


        // #endif

        let hideTabbarBool = false;
        let hideTabbarTime = setInterval(() => {
            if (hideTabbarBool) {
                clearInterval(hideTabbarTime);
            } else {
                uni.hideTabBar({
                    complete: ((res) => {
                        // console.log(res);
                        if (res.errMsg == "hideTabBar:ok") {
                            hideTabbarBool = true;
                        }
                    })
                });
            }
        }, 100);
        console.log('App Launch')
    },
    onShow: function () {
        uni.hideTabBar();
        console.log('App Show');
    },
    onHide: function () {
        console.log('App Hide')
    },
    methods: {
        //上传图片 count-选择图片数量
        async imgUpload(count, callback) {
            const that = this;
            uni.chooseImage({
                count: count, //默认9
                sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera '], //从相册选择
                success: async function (chooseImageRes) {
                    // console.log(JSON.stringify(chooseImageRes.tempFilePaths));
                    const tempFilePaths = chooseImageRes.tempFilePaths;
                    // console.log(tempFilePaths);
                    that.$comFun.openLoading();
                    let img_list = [];
                    for (var i = 0; i < tempFilePaths.length; i++) {
                        let res = await that.$request.uploadFile(tempFilePaths[i]);
                        // console.log(res);
                        if (res.code == 200) {
                            img_list.push(res.result);
                        }
                    }
                    that.$comFun.closeLoading();
                    callback(img_list);
                },
            });
        },
        //上传图片-会返回图片信息 count-选择图片数量
        async imgUploadHasInfo(count, callback) {
            const that = this;
            uni.chooseImage({
                count: count, //默认9
                sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera '], //从相册选择
                success: async function (chooseImageRes) {
                    // console.log(JSON.stringify(chooseImageRes.tempFilePaths));
                    const tempFilePaths = chooseImageRes.tempFilePaths;
                    // console.log(tempFilePaths);
                    that.$comFun.openLoading();
                    let img_list = [];
                    for (var i = 0; i < tempFilePaths.length; i++) {
                        let res = await that.$request.uploadFileHasInfo(tempFilePaths[i]);
                        // console.log(res);
                        if (res.code == 200) {
                            img_list.push(res.result);
                        }
                    }
                    that.$comFun.closeLoading();
                    callback(img_list);
                },
            });
        },
        async initAppUpdate() {
            const that = this;
            const res = await this.$request.get('public/version', {});
            // console.log(res);
            // console.log(plus.runtime);
            if (res.code == 200) {
                plus.runtime.getProperty(plus.runtime.appid, function (wgtinfo) {
                    // console.log(wgtinfo.versionCode);
                    const versionCode = wgtinfo.versionCode;
                    // const versionCode = 100;
                    // this.runtime = plus.runtime.versionCode + ',' + res.result.versionCode + ',' + (Number(res.result.versionCode) > Number(versionCode));
                    // console.log(this.runtime);
                    if (Number(res.result.versionCode) > Number(versionCode)) {
                        if (that.$store.state.deviceType == 'android') {
                            that.$store.dispatch('setAppUpdate', res.result.android_url)
                        } else if (that.$store.state.deviceType == 'ios') {
                            that.$store.dispatch('setAppUpdate', res.result.ios_url)
                        }
                    } else {
                        that.$store.dispatch('setAppUpdate', false)
                    }
                });
            }
        },
    }
}
</script>

<style lang="scss">
/*每个页面公共css */
@import "@/common/css/public.scss";
@import "@/common/css/iconfont_base/iconfont.css";
@import "@/common/css/iconfont_app/iconfont.css";
</style>
